package secret.hehe.com.secret.Activity;

import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;

import secret.hehe.com.secret.R;

import static secret.hehe.com.secret.Util.Util.getPath;
import static secret.hehe.com.secret.Util.Util.setImageSize;

/**
 * Created by DELL on 2018/2/28.
 */

public class SendHiddenActivity extends AppCompatActivity {
    private int hidden_type;
    private EditText hidden_text;
    private PopupWindow hiddenType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sendhidden);

        hidden_text = (EditText)findViewById(R.id.send_hidden);
        Intent intent = getIntent();
        try {
            hidden_type =(int) intent.getSerializableExtra("hidden_type");
            hidden_text.setText((String) intent.getSerializableExtra("hidden_text"));
        }catch (Exception e){
            e.printStackTrace();
        }

        //添加图片
        ImageView add_image = (ImageView)findViewById(R.id.add_hidden_image);
        add_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                insertImage();
            }
        });
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_hidden, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
            case R.id.hidden_delete:{//删除隐藏信息
                Intent intent = new Intent();
                intent.putExtra("hidden_type",1);
                intent.putExtra("hidden_text","");
                intent.putExtra("isHidden",0);
                intent.putExtra("keybody","");
                setResult(1002, intent);
                this.finish();
                return true;
            }
            case R.id.hidden_finish:{//确定隐藏信息
                final Intent intent = new Intent();
                intent.putExtra("hidden_type",1);
                intent.putExtra("hidden_text",hidden_text.getText().toString());
                if(hidden_text.getText().toString().equals("")){
                    intent.putExtra("isHidden",0);
                }else{
                    intent.putExtra("isHidden",1);
                }
                    setResult(1002, intent);
                    this.finish();
                    return true;
            }
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //收到图片
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000 && resultCode == RESULT_OK) {
            //相册
            final Uri selectedImage = data.getData();
            View customView = getLayoutInflater().inflate(R.layout.menu_hidden, null, false);
            hiddenType = new PopupWindow(customView, 215,178);
            hiddenType.setOutsideTouchable(false);
            //图片隐藏文字
            Button textType = (Button) findViewById(R.id.set_hidden_type_text);
            textType.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    View customView = getLayoutInflater().inflate(R.layout.menu_hidden_text, null, false);
                    final PopupWindow hiddenText = new PopupWindow(customView, 215,178);
                    hiddenText.setOutsideTouchable(false);
                    Button setHiddenTextFinish = (Button) findViewById(R.id.set_hidden_text_finish);
                    setHiddenTextFinish.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //完成按钮
                            EditText imgHiddenText = (EditText) findViewById(R.id.set_hidden_text);
                            insertImfo(selectedImage,imgHiddenText.getText().toString());
                            if (hiddenType != null && hiddenType.isShowing()) {
                                hiddenType.dismiss();
                            }
                        }
                    });
                    Button setHiddenTextDelete = (Button) findViewById(R.id.set_hidden_text_delete);
                    setHiddenTextDelete.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //取消按钮
                            if (hiddenText != null && hiddenText.isShowing()) {
                                hiddenText.dismiss();
                            }
                        }
                    });
                }
            });

            Button noHidden = (Button) findViewById(R.id.set_no_hidden);
            noHidden.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    insertImfo(selectedImage,"");
                    if (hiddenType != null && hiddenType.isShowing()) {
                        hiddenType.dismiss();
                    }
                }
            });

        }
    }

    private void insertImage(){
        //插入图片
        try {
            Intent intent = new Intent(Intent.ACTION_PICK);
            //选择图片格式
            intent.setType("image/*");
            intent.putExtra("return-data",true);
            startActivityForResult(intent, 1000);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void insertImfo(Uri selectedImage,String hidden_value){
        //EditText中插入图片
        try {
            //图片内隐藏信息

            String image_path =  getPath(getContentResolver(),selectedImage);
            Bitmap loadImage = setImageSize(image_path,hidden_text);
            ImageSpan imageSpan = new ImageSpan(getApplicationContext(), loadImage);


            String tempUrl = "<img src=\"" + image_path + "\" alt=\"[{"+hidden_value+"}]\" />";
            SpannableString spannableString = new SpannableString(tempUrl);
            spannableString.setSpan(imageSpan, 0, tempUrl.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            // 将选择的图片追加到EditText中光标所在位置
            int index = hidden_text.getSelectionStart();
            // 获取光标所在位置
            Editable edit_text = hidden_text.getEditableText();
            if (index < 0 || index >= edit_text.length()) {
                edit_text.append(spannableString);
            } else {
                edit_text.insert(index, spannableString);
            }
            //换行
            edit_text.insert(index + spannableString.length(), "\n");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
